﻿package mortal.game.view.copy.towerCopy.other
{
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.copy.towerCopy.*;

    public class TowerPassInfoItem extends GSprite
    {
        public var MouseOver:Function;
        public var MouseOut:Function;
        private var _passIndex:int;
        private var _passed:Boolean;
        private var bitmaps:Vector.<String>;
        private var bitmapsRect:Vector.<Object>;
        private var btimapIndex:int;
        private var tween1:TweenMax;
        private var tween2:TweenMax;
        private var _roleModelPlayer:RoleModelPlayer;
        private var _img2D:Img2D;
        private var _judgeRec:Rectangle;
        private var _imgX:Number;
        private var _imgY:Number;
        protected var _window:Window;

        public function TowerPassInfoItem(param1:TowerModule)
        {
            this.bitmaps = new Vector.<String>;
            this.bitmapsRect = new Vector.<Object>;
            this._judgeRec = new Rectangle(0, 0, 208, 220);
            this._window = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.bitmaps.push(ImagesConst.tower_win_pass_0_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_0_1_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_1_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_1_1_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_2_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_2_1_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_3_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_3_1_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_4_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_4_1_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_5_0_upSkin);
            this.bitmaps.push(ImagesConst.tower_win_pass_5_1_upSkin);
            this.bitmapsRect.push({width:"43", height:"41"});
            this.bitmapsRect.push({width:"50", height:"42"});
            this.bitmapsRect.push({width:"122", height:"106"});
            this.bitmapsRect.push({width:"124", height:"109"});
            this.bitmapsRect.push({width:"122", height:"127"});
            this.bitmapsRect.push({width:"133", height:"139"});
            this.bitmapsRect.push({width:"120", height:"121"});
            this.bitmapsRect.push({width:"124", height:"119"});
            this.bitmapsRect.push({width:"121", height:"152"});
            this.bitmapsRect.push({width:"128", height:"157"});
            this.bitmapsRect.push({width:"147", height:"196"});
            this.bitmapsRect.push({width:"152", height:"200"});
            this.btimapIndex = -1;
            StageMouseManager.instance.addRecEvent(this._judgeRec, this.onMouseOverItem, this.onMouseOutItem);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseMoveHandler);
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMoveHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            StageMouseManager.instance.removeRecEvent(this._judgeRec);
            this.removeDrag();
            this.bitmaps.length = 0;
            this.killTween();
            this.tween1 = null;
            this.tween2 = null;
            var _loc_2:* = this.get3DRect();
            if (_loc_2)
            {
                if (this._img2D)
                {
                    _loc_2.removeImg(this._img2D);
                    this._img2D = null;
                }
                if (this._roleModelPlayer)
                {
                    _loc_2.disposeObj3d(this._roleModelPlayer);
                    this._roleModelPlayer = null;
                }
            }
            this.btimapIndex = -1;
            return;
        }// end function

        private function get3DRect() : Rect3DObject
        {
            return TowerModule(this._window).get3DObject();
        }// end function

        public function refresh(param1:Boolean = false) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            this.reCreateImg2D();
            this.img.x = this.imgX;
            this.img.y = this.imgY;
            this.updatejudgeRec();
            if (this._roleModelPlayer)
            {
                this.get3DRect().disposeObj3d(this._roleModelPlayer);
                this._roleModelPlayer = null;
            }
            if (this.passIndex == (Cache.instance.copy.tower.getCurrentLevel(TowerModule.copyCode) - 1))
            {
                this._roleModelPlayer = FrObjectPool.getObject(RoleModelPlayer, this);
                this._roleModelPlayer.entityInfo = Cache.instance.role.roleEntityInfo;
                this._roleModelPlayer.scaleAll = 0.5;
                this._roleModelPlayer.useLight();
                this._roleModelPlayer.rotationX = 0;
                this._roleModelPlayer.rotationY = 0;
                _loc_2 = this.imgX + this.bitmapsRect[this.btimapIndex].width * 0.5 - 2;
                _loc_3 = this.imgY + this.bitmapsRect[this.btimapIndex].height * 0.5 - 10;
                if (this.passIndex == 14)
                {
                    _loc_3 = _loc_3 + 20;
                }
                else if (this.passIndex == 19)
                {
                    _loc_2 = _loc_2 - 10;
                    _loc_3 = _loc_3 + 20;
                }
                this.get3DRect().addObject3d(this._roleModelPlayer, _loc_2, _loc_3);
            }
            if (this.passed)
            {
                this.killTween();
            }
            else
            {
                this.killTween();
            }
            this.shakeTween(200);
            return;
        }// end function

        public function set imgX(param1:Number) : void
        {
            this._imgX = param1;
            return;
        }// end function

        public function get imgX() : Number
        {
            return this._imgX;
        }// end function

        public function set imgY(param1:Number) : void
        {
            this._imgY = param1;
            return;
        }// end function

        public function get imgY() : Number
        {
            return this._imgY;
        }// end function

        public function get imgWidth() : Number
        {
            return this._judgeRec.width;
        }// end function

        public function get imgHeight() : Number
        {
            return this._judgeRec.height;
        }// end function

        private function get imgGlobal() : Point
        {
            var _loc_1:* = TowerModule(this._window);
            var _loc_2:* = _loc_1.rect;
            var _loc_3:* = _loc_1.localToGlobal(new Point(_loc_2.x, _loc_2.y));
            _loc_3.x = _loc_3.x + this.img.x;
            _loc_3.y = _loc_3.y + this.img.y;
            return _loc_3;
        }// end function

        private function updatejudgeRec() : void
        {
            var _loc_1:Point = null;
            if (this._img2D)
            {
                _loc_1 = this.imgGlobal;
                this._judgeRec.x = _loc_1.x;
                this._judgeRec.y = _loc_1.y;
            }
            return;
        }// end function

        private function get img() : Img2D
        {
            if (this._img2D == null)
            {
                this.reCreateImg2D();
            }
            return this._img2D;
        }// end function

        private function reCreateImg2D() : void
        {
            var _loc_2:BitmapData = null;
            var _loc_3:Bitmap = null;
            var _loc_4:BitmapData = null;
            var _loc_1:* = this.get3DRect();
            if (this._img2D)
            {
                _loc_1.removeImg(this._img2D);
                this._img2D = null;
            }
            if (this.passed)
            {
                _loc_3 = new Bitmap(GlobalClass.getBitmapData(this.bitmaps[this.btimapIndex]));
                _loc_3.filters = [FilterConst.colorFilter2];
                _loc_4 = new BitmapData(_loc_3.width, _loc_3.height, true, 0);
                _loc_4.draw(_loc_3);
                _loc_2 = _loc_4;
            }
            else
            {
                _loc_2 = GlobalClass.getBitmapData(this.bitmaps[this.btimapIndex]);
            }
            this._judgeRec.width = _loc_2.width;
            this._judgeRec.height = _loc_2.height;
            this._img2D = new Img2D(null, _loc_2, new Rectangle(0, 0, this.bitmapsRect[this.btimapIndex].width, this.bitmapsRect[this.btimapIndex].height), true);
            _loc_1.addImg(this._img2D, Layer3DManager.BackGroudEffectLayer);
            return;
        }// end function

        public function set passIndex(param1:int) : void
        {
            this._passIndex = param1;
            return;
        }// end function

        public function get passIndex() : int
        {
            return this._passIndex;
        }// end function

        public function set passed(param1:Boolean) : void
        {
            this._passed = param1;
            var _loc_2:* = this._passIndex % 5;
            if (_loc_2 == 0)
            {
                this.btimapIndex = 2;
            }
            else if (_loc_2 == 1)
            {
                this.btimapIndex = 4;
            }
            else if (_loc_2 == 2)
            {
                this.btimapIndex = 6;
            }
            else if (_loc_2 == 3)
            {
                this.btimapIndex = 8;
            }
            else if (_loc_2 == 4)
            {
                this.btimapIndex = 10;
            }
            else
            {
                this.btimapIndex = 0;
            }
            if (this._passed)
            {
                (this.btimapIndex + 1);
            }
            return;
        }// end function

        public function get passed() : Boolean
        {
            return this._passed;
        }// end function

        private function onMouseOverItem() : void
        {
            if (this.MouseOver != null && this.passIndex != -1)
            {
                this.MouseOver(this);
            }
            return;
        }// end function

        private function onMouseOutItem() : void
        {
            if (this.MouseOut != null && this.passIndex != -1)
            {
                this.MouseOut(this);
            }
            return;
        }// end function

        private function onStageMouseMoveHandler(event:MouseEvent) : void
        {
            if (this.passIndex != -1)
            {
                this.updatejudgeRec();
            }
            return;
        }// end function

        private function onStageMouseUpHandler(event:MouseEvent) : void
        {
            if (this.passIndex != -1)
            {
                this.removeDrag();
            }
            return;
        }// end function

        private function removeDrag() : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onStageMouseMoveHandler);
            return;
        }// end function

        private function shakeTween(param1:int) : void
        {
            return;
        }// end function

        private function killTween() : void
        {
            if (this.tween1 && this.tween2)
            {
                this.tween1.kill();
                this.tween2.kill();
            }
            return;
        }// end function

    }
}
